using System;
using System.Data;
using System.Windows.Forms;
using TUnit.Data;
using TUnit.Class;
namespace viclip
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CPackages : Class
    {
        #region Declares
        int _packageId = -1;
        string _packageName = "";
        CCrossWord _crossWord = null;
        int _countOpen = -1;
        #endregion

        #region Constructors
        public CPackages()
        {
        }
        public CPackages(CLogin Login)
        {
            base.Login = Login;
        }
        public CPackages(CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBPackages db = new AccDBPackages())
            {
                this.Populate((CPackages)db.GetClass(Login, ObjId));
            }
        }
        public CPackages(CLogin Login, IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CPackages(CLogin Login, IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBPackages db = new AccDBPackages())
            {
                this.Populate((CPackages)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("packageId") >= 0)
                    {
                        this._packageId = (dr["packageId"] != DBNull.Value ? Convert.ToInt32(dr["packageId"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblPackages ADD packageId [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("packageName") >= 0)
                    {
                        this._packageName = (dr["packageName"] != DBNull.Value ? Convert.ToString(dr["packageName"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblPackages ADD packageName [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("crossWordId") >= 0)
                    {
                        this._crossWord = new CCrossWord(Login, p, (dr["crossWordId"] != DBNull.Value ? Convert.ToInt32(dr["crossWordId"]) : -1));
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblPackages ADD crossWordId [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("countOpen") >= 0)
                    {
                        this._countOpen = (dr["countOpen"] != DBNull.Value ? Convert.ToInt32(dr["countOpen"]) : 0);
                    }
                }
                catch { }
            }
        }
        public void Populate(CPackages Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._packageId = Obj.packageId;
                    this._packageName = Obj.packageName;
                    this._crossWord = Obj.crossWord;
                    this._countOpen = Obj.countOpen;
                }
                catch { }
            }
        }
        public override bool Save()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBPackages db = new AccDBPackages())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(IProvider p)
        {
            using (AccDBPackages db = new AccDBPackages())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBPackages db = new AccDBPackages())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(IProvider p)
        {
            using (AccDBPackages db = new AccDBPackages())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CPackages obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CPackages Packages);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CPackages Packages);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CPackages Packages);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CPackages Packages);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CPackages obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int packageId
        {
            get { return this._packageId; }
            set
            {
                if (this._packageId != value)
                {
                    this._packageId = value;
                }
            }
        }
        public string packageName
        {
            get { return this._packageName; }
            set
            {
                if (this._packageName != value)
                {
                    this._packageName = value;
                }
            }
        }
        public CCrossWord crossWord
        {
            get { return this._crossWord; }
            set
            {
                if (this._crossWord != value)
                {
                    this._crossWord = value;
                }
            }
        }
        public int countOpen
        {
            get { return this._countOpen; }
            set
            {
                if (this._countOpen != value)
                {
                    this._countOpen = value;
                }
            }
        }

        CCollection _QuestionsStarts = null;
        public CCollection QuestionsStarts
        {
            get
            {
                if (_QuestionsStarts == null)
                {
                    using (AccDBQuestion db = new AccDBQuestion())
                    {
                        CCollection tmp = new CCollection();
                        db.GetCollectionStarts(this, tmp);
                        this._QuestionsStarts = tmp;
                    }
                }
                return this._QuestionsStarts;
            }
        }

        CCollection _QuestionsSpeed = null;
        public CCollection QuestionsSpeed
        {
            get
            {
                if (_QuestionsSpeed == null)
                {
                    using (AccDBQuestion db = new AccDBQuestion())
                    {
                        CCollection tmp = new CCollection();
                        db.GetCollectionSpeed(this, tmp);
                        this._QuestionsSpeed = tmp;
                    }
                }
                return this._QuestionsSpeed;
            }
        }

        CCollection _QuestionsEnd = null;
        public CCollection QuestionsEnd
        {
            get
            {
                if (_QuestionsEnd == null)
                {
                    using (AccDBQuestion db = new AccDBQuestion())
                    {
                        CCollection tmp = new CCollection();
                        db.GetCollectionEnd(this, tmp);
                        this._QuestionsEnd = tmp;
                    }
                }
                return this._QuestionsEnd;
            }
        }
        #endregion
    }

    #region Class AccDB
    public class AccDBPackages : AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBPackages()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(CLogin Login, ComboBox cmb, string extName)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT PackagesId, PackagesName";
                sql += " FROM  tblPackages";
                sql += " WHERE (PackagesId >= 0)";
                sql += " ORDER BY PackagesName";
                DataTable table = p.OpenTable(sql, "tblPackages");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["PackagesName"] = extName;
                        row["PackagesId"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "PackagesName";
                    cmb.ValueMember = "PackagesId";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override Class GetClass(CLogin Login, IProvider p, object ObjId)
        {
            try
            {
                if (ObjId is int)
                {
                    string sql = "SELECT * FROM tblPackages WHERE (PackagesId = " + ObjId + ")";
                    DataTable table = p.OpenTable(sql, "tblPackages");
                    if ((table != null) && (table.Rows.Count > 0))
                    {
                        return new CPackages(Login, p, table.Rows[0]);
                    }
                }
                else
                {
                    if (ObjId is CAccount)
                    {
                        //lay ngau nhien mot bo cau hoi
                        string sql = "SELECT tblPackages.*";
                        sql += " FROM tblPackages LEFT OUTER JOIN";
                        sql += " tblAccAndPackages ON tblPackages.packageId = tblAccAndPackages.packageId";
                        sql += " WHERE (tblAccAndPackages.email IS NULL OR tblAccAndPackages.email <>'{0}')";
                        sql += " AND (tblPackages.gameLevelId ={1})";
                        sql = String.Format(sql, ((CAccount)ObjId).email, ((CAccount)ObjId).GameLevelCurrent.gameLevelId);
                        DataTable table = p.OpenTable(sql, "tblPackages");
                        if ((table != null) && (table.Rows.Count > 0))
                        {
                            return new CPackages(Login, p, table.Rows[0]);
                        }
                    }
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(IProvider p, Class obj)
        {
            CPackages Packages = (CPackages)obj;
            string sql = "SELECT * FROM tblPackages";
            sql += " WHERE PackagesId =" + Packages.packageId + "";
            DataTable dt = p.OpenTable(sql, "tblPackages");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1 && Packages.packageId != -1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    Packages.packageId = Convert.ToInt32(p.InitIDKey("packageId", "tblPackages", null));
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["packageId"] = Packages.packageId;
                row["packageName"] = Packages.packageName;
                row["crossWordId"] = Packages.crossWord.crossWordId;
                row["countOpen"] = Packages.countOpen;
                if (!p.UpdateTable("tblPackages", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(IProvider p, Class obj)
        {
            CPackages Packages = (CPackages)obj;
            string sql = "DELETE FROM tblPackages";
            sql += " WHERE PackagesId =" + Packages.packageId + "";
            //sql +=" AND CompanyId =" + Packages.CompanyId + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(CLogin Login, IProvider p, CCollection Collection)
        {
            string sql = "SELECT * FROM tblPackages ORDER BY PackagesName";
            DataTable table = p.OpenTable(sql, "tblPackages");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CPackages o = new CPackages(Login, p, row);
                    Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(IProvider p, Class obj, CCollection Collection)
        {
        }
    }
    #endregion
}//40:10:4 60:10:2-20:2 80:10:1:20:2:30:1 
